const { show } = require('../game/alert');
let { foodMaterials, shopData } = require('../shop/gameData')
cc.Class({
    extends: cc.Component,

    properties: {
        goods:cc.Object,
        cangku:cc.Label,
        ice:cc.Label,
        number:cc.Label
    },

    // LIFE-CYCLE CALLBACKS:

    onEnable () {
        this.buyMaterial()
    },
    buyMaterial(){
       this.goods =  JSON.parse(JSON.stringify(foodMaterials.find(val=>{
        return val.id == material.id;
     })))
        
    },
    ensure(target,id){
        //查看库存和钱够不够
        let shopId = target.target.parent.id;
        let shop = shopData.shopList.find(val=>val.id==shopId);
        let money = this.goods.count*this.goods.cost;
        cc.log(this.goods.ice)
        if(shop.money<money){
            show('订单提醒',"您公司的余额不足");
            return ;
        }
        let space = this.goods.stock==0?this.goods.ice*this.goods.count:this.goods.stock*this.goods.count;
        show('订单确认',`将花费${money}元,占空间${space}`,(res=>{
            cc.log(res)
            if(money==0){
                return ;
            }else{
                            //查看库存
                if(this.goods.ice!=0){
                    //说明是普通货物
                    if(shop.leftIce>=space){
                        let material = shop.stock.find(val=>val.id==this.goods.id);
                        shop.leftIce -=space;
                            shop.stock.push(JSON.parse(JSON.stringify(this.goods)));
                            this.goods.count = 0;
                    }else{
                        setTimeout(()=>{
                            show('仓库反馈',"店铺仓库放不下啦！")
                        },400)
                        
                    }
                }else{
                    if(shop.leftStock>=space){
                        let material = shop.stock.find(val=>val.id==this.goods.id);
                        shop.leftStock -=space;
                            shop.stock.push(JSON.parse(JSON.stringify(this.goods)));
                            this.goods.count = 0;
                            this.number.string=0;
                    }else{
                        setTimeout(()=>{
                            show('仓库反馈',"店铺仓库放不下啦！")
                        },400)
                        
                    }
                }
                        shop.money-=money;
                        this.cangku.string = shop.leftStock;
                        this.ice.string = shop.leftIce;
            }
        }))
        

        
        
    },
    add(){
        cc.log(this.goods);
        
        this.goods.count+=10;
        if(this.goods.count>999){this.goods.count=999};

        this.number.string=this.goods.count;
    },
    reduce(){
        this.goods.count-=10;
        if(this.goods.count<0){this.goods.count=0};
        this.number.string=this.goods.count;
    },
    calStock(){
        cc.log(this.goods)
    },
    start () {

    },

    // update (dt) {},
});
